/*
 * @Author: xishanxu
 * @LastEditors: xisuanxu
 * @LastEditTime: 2019-12-21 21:45:15
 */
import { testAjax } from '@/utils/request';

export interface LoginParamsType {
  username: string;
  password: string;
  mobile: string;
  captcha: string;
  type: string;
  verificationKey: string;
  verificationCode: string;
}

// 获取登录验证码
export async function getCaptcha(mobile: string) {
  return testAjax(`/login/captcha?mobile=${mobile}`);
}

// TODO: 1、登录获取授权（token）
export function getAuthorizations(params: LoginParamsType) {
  if (params.type !== 'mobile') {
    // 密码登录
    return testAjax('/admin/authorizations', {
      method: 'POST',
      data: {
        username: params.username,
        password: params.password,
      },
      headers: { 'Content-Type': 'application/json' },
    });
  }
  // 验证码登录
  return testAjax('/admin/sms/authorizations', {
    method: 'POST',
    data: {
      phone: params.mobile,
      verification_key: params.verificationKey,
      verification_code: params.verificationCode,
    },
    headers: { 'Content-Type': 'application/json' },
  });
}
